package com.aaa.controller;

import com.aaa.pojo.Change;
import com.aaa.pojo.Invest;
import com.aaa.pojo.Pay_Money;
import com.aaa.pojo.Product_type;
import com.aaa.service.InvestService;
import com.aaa.service.Pay_MoneyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(name = "工资理财",value = "/pay_Money")
public class Pay_Money_Controller {
    @Autowired
    private Pay_MoneyService pay_money;
    @Autowired
    private InvestService invest;
    @RequestMapping(value = "/zy")
    public String zy(){
        return "views/Pay_Money";
    }
    @ResponseBody
    @RequestMapping(value = "/selectPay_Money/{currentPage}/{pageSize}",method = RequestMethod.GET)
    public PageInfo<Pay_Money> getUserByUserPage(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize){
        return pay_money.selectPay_Money(currentPage,pageSize);
    }
    @RequestMapping(name = "添加",value = "/insert")
    public String insert(Integer pro_id, BigDecimal pay_amount, double pay_rate,Integer h_id){
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = df.format(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_WEEK, h_id);
        String format1 = df.format(calendar.getTime());
        String s = format + "~" + format1;
        //添加
        invest.insertInvest(new Invest(s, h_id + ""));
        //查询
        Invest invest1=invest.selectDate(s);
        Integer h_id1 = invest1.h_id;
        //添加
        pay_money.insertPay_Money(new Pay_Money(pro_id,pay_amount,pay_rate,h_id1));
        return "redirect:zy";
    }
    @RequestMapping(name = "查询下拉框",value = "/type")
    @ResponseBody
    public Map<String,Object> selectProduct_type(){
        List<Product_type> product_types = pay_money.selectProdcut_type();
        Map<String,Object> map = new HashMap<>();
        map.put("type",product_types);
        return map;
    }


    @RequestMapping(name = "根据id查询",value = "/selectById")
    @ResponseBody
    public Map<String,Object> selectById(Integer pay_id){
        System.out.println(pay_id+"=====aaaaa");
        Map<String,Object> map = new HashMap<>();
        map.put("byId",pay_money.selectById(pay_id));
        return  map;
    }

    @RequestMapping(name = "修改",value = "/update")
    public String update(Integer pro_id, BigDecimal pay_amount, double pay_rate, Integer h_id,Integer pay_id){
        System.out.println(pay_id+"======aaaaa");
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = df.format(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_WEEK, h_id);
        String format1 = df.format(calendar.getTime());
        String s = format + "~" + format1;
        pay_money.updatePay_Money(new Pay_Money(pro_id,pay_amount,pay_rate,s,h_id+"",pay_id));
        return "redirect:zy";
    }
    @RequestMapping(name = "删除",value = "/delete")
    @ResponseBody
    public PageInfo<Pay_Money> delete(Integer pay_id){
        System.out.println(pay_id+"=====aaaaa");
        boolean b = pay_money.deletePay_Money(pay_id);
        PageInfo<Pay_Money> allUsersByPage = pay_money.selectPay_Money(0, 5);
        return allUsersByPage;
    }
}
